package com.zx.erp.purchase.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zx.erp.purchase.bean.CBrand;
import com.zx.erp.purchase.bean.CFirm;
import com.zx.erp.purchase.bean.CProduct;
import com.zx.erp.purchase.bean.CType;
import com.zx.erp.purchase.mapper.CBrandMapper;
import com.zx.erp.purchase.mapper.CProductMapper;
import com.zx.erp.purchase.mapper.CTypeMapper;
import com.zx.erp.purchase.service.CBrandService;
import com.zx.erp.purchase.service.CFirmService;
import com.zx.erp.purchase.service.CProductService;
import com.zx.erp.purchase.service.CTypeService;
import com.zx.erp.sys.bean.Dept;
import com.zx.erp.sys.bean.Users;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 作者信息
 * @since 2021-11-10
 */
@Controller
@RequestMapping("/purchase/cproduct")
public class CProductController {
    @Autowired
    private CProductService cProductService;
    @Autowired
    private CBrandService cBrandService;
    @Autowired
    private CTypeService cTypeService;
    @Autowired
    private CFirmService cFirmService;
    @Autowired
    private CProductMapper cProductMapper;
    @Autowired
    private CBrandMapper cBrandMapper;
    @Autowired
    private CTypeMapper cTypeMapper;
    @RequestMapping("/cplist")
    public String findByPage(Model model, CProduct cProduct, @RequestParam(value = "pageno", defaultValue = "1") Integer
            pageNo) {
        Integer pageSize = 6;
        Page<CProduct> pageInfo = cProductService.selectP(cProduct, pageNo, pageSize);
        if (cProduct.getBrandId()!=null){
            CBrand cBrand = cBrandMapper.selectById(cProduct.getBrandId());
            model.addAttribute("cBrand", cBrand);
        }
        if (cProduct.getTypeId()!=null){
            CType cType = cTypeMapper.selectById(cProduct.getTypeId());
            model.addAttribute("cType", cType);
        }
        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("CProduct", cProduct);
        return "forward:/purchase/product/productList.html";
    }
    /*查品牌*/
    @RequestMapping("/selectpai")
    @ResponseBody
    public List<CBrand> selectpai(){
        List<CBrand> list = cBrandService.list();
        return list;
    }
    /*查找类型*/
    @RequestMapping("/selecttype")
    @ResponseBody
    public List<CType> selecttype(CType cType){
        List<CType> list = cTypeService.selecttype(cType);
        return list;
    }
    /*查询厂商*/
    @RequestMapping("/chang")
    @ResponseBody
    public List<CFirm> chang(){
        List<CFirm> list = cFirmService.list();
        return list;
    }
    /*添加*/
    @RequestMapping("/doadd")
    public String doadd(CProduct cProduct){
        boolean save = cProductService.save(cProduct);
        return "forward:/purchase/cproduct/cplist";
    }
    /*修改回显数据*/
    @RequestMapping("/goupdate")
    public String goupdate(CProduct cProduct,String nname,Model model){
        CProduct cProduct1 = cProductMapper.selectById(cProduct);
        model.addAttribute("cproduct",cProduct1);
        model.addAttribute("name",nname);
        return "forward:/purchase/product/productUpdate.html";
    }
    /*修改*/
    @RequestMapping("/update")
    public String update(CProduct cProduct,CType cType){
        boolean b = cProductService.updateById(cProduct);
        boolean b1 = cTypeService.updateById(cType);
        return "forward:/purchase/cproduct/cplist";
    }
    /*注销和恢复*/
    @RequestMapping("/remove")
    public String remove(CProduct cProduct){
        if (cProduct.getProductStatus()==1){
            cProduct.setProductStatus(2);
        }else {
            cProduct.setProductStatus(1);
        }
        boolean b = cProductService.updateById(cProduct);
        return "redirect:/purchase/cproduct/cplist";
    }
}

